$(function () {
    useLayUI();


    $("#infoButton").click(function(){
        var img = $('input[name="img"]').val()
        var name = $('input[name="name"]').val()
        var email = $('input[name="email"]').val()
        var companyFullName = $('input[name="companyFullName"]').val()
        var position = $('#position').val()
        if(img == "" || img == null){
            layerMsg("图片未上传或正在上传中")
            return false;
        }
        if(name == "" || name == null){
            layerMsg("名称不为空")
            return false;
        }
        if(email == "" || email == null){
            layerMsg("邮箱不为空")
            return false;
        }
        if(companyFullName == "" || companyFullName == null){
            layerMsg("公司全名不为空")
            return false;
        }

        $.ajax({
            type: 'post', // 提交方式 get/post
            url: companyServices+"/user/reg", // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
				withCredentials: true
			}
            ,crossDomain: true
            ,data:JSON.stringify({
                "user":{
                    "name": name,
                    "img": img,
                    "gender": 2,
                    "position": position,
                    "companyId": "",
                    "birthday": "",
                    "idNum": "",
                    "email": email
                },
                "companyFullName":companyFullName
            }),
             success: function(res) {
                if(res.success){
                    data = res.data;
                    $.cookie('userInfo',JSON.stringify(data.user))
                    $.cookie('fullName',companyFullName);
                    // $.cookie('id', data.id)
                    // $.cookie('name', data.name)
                    // $.cookie('gender: ', data.gender)
                    // $.cookie('birthday', data.birthday)
                    // $.cookie('firstWorkTime', data.firstWorkTime)
                    // $.cookie('img', data.img)
                    // $.cookie('tel', data.tel)
                    layerMsg(res.message);
                    setTimeout(function() {
                        if (data.company != null) {
                            $.cookie('companyInfo',JSON.stringify(data.company))
                            window.location.href="regi-join-company.html" ;
                        } else {
                            window.location.href="regi-company-info.html" ;
                        }

                    },2000);
                    
                }else{
                    layerMsg(res.message)
                }               
            },
        })
    });

})
//上传图片
function uploadImg(str){
    var fileSize = $('#file')[0].files[0].size;
    var size = fileSize / 1024;
    if(size>5000){
        layerMsg("图片不超于5M");
        return false;
    }
    layer.load(2);

    var file = $('#file').val();
    
    var option = {
        type: 'post', // 提交方式 get/post
        url: companyServices+"/user/upload/img", // 需要提交的 url
        data: {
        'file': file,
         },
         success: function(res) {
            layer.closeAll('loading');
            var obj = JSON.parse(res);
            if(obj.code==0){
             //    console.log(obj.data)
                var fullPath = companyServices + obj.data.src;
                $('#'+str).attr('src',fullPath);
                $('#img').val(obj.data.src) ;
                layerMsg(obj.msg);
            }
        }
    }
    $("#uploadImgform").ajaxSubmit(option);
        
}